
function filter(treeId, parentNode, childNodes) {
    if (!childNodes) return null;
    for (var i=0, l=childNodes.length; i<l; i++) {
        childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
    }
    return childNodes;
}
function beforeRemove(treeId, treeNode) {
    if(treeNode.pId==''||treeNode.pId==null ||treeNode.pId==0){
        alert("根节点不可以删除");
        return false;
    }
    var zTree = $.fn.zTree.getZTreeObj("tree");
    zTree.selectNode(treeNode);
    return confirm("确认删除 节点 -- " + treeNode.name + " 吗？");
}
function beforeRename(treeId, treeNode, newName) {
    if (newName.length == 0) {
        setTimeout(function() {
            var zTree = $.fn.zTree.getZTreeObj("tree");
            zTree.cancelEditName();
            alert("节点名称不能为空.");
        }, 0);
        return false;
    }
    return true;
}

var newCount = 1;
function addHoverDom(treeId, treeNode) {
    var sObj = $("#" + treeNode.tId + "_span");
    if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0) return;
    var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
        + "' title='添加部门' onfocus='this.blur();'></span>";
    // if(!treeNode.pId){
    //     sObj.after(addStr);
    // }
    sObj.after(addStr);
    var btn = $("#addBtn_"+treeNode.tId);
    if (btn) btn.bind("click", function(){
        layer.confirm("要添加部门?",{
            btn: ['添加下属部门','取消'],

            btn1:function () {
                var zTree = $.fn.zTree.getZTreeObj("tree");
                layer_show('添加部门',bathPath+'/groupAddInitPage.htmlx?pId='+treeNode.id,'','510');
              //  zTree.addNodes(treeNode, {id:'', pId:treeNode.id, name:"新建部门1"});
            },
            bnt2:function () {
                layer.close(0);
            }
           });

    });


};

function removeHoverDom(treeId, treeNode) {
    $("#addBtn_"+treeNode.tId).unbind().remove();
};

function remove(e,treeId, treeNode) {
    var zTree = $.fn.zTree.getZTreeObj("tree");
    var id = treeNode.id;
    var pId = treeNode.pId;
        if(id){
            $.post(bathPath+"/deleteGroupById.htmlx",{id:id,pId:pId},function (data) {

            });
        }
        var callbackFlag = $("#callbackTrigger").attr("checked");
        zTree.removeNode(treeNode, callbackFlag);

};

function rename(e,treeId, treeNode) {
    var zTree = $.fn.zTree.getZTreeObj("tree");
  //  var treeNode = nodes[0];
    var id = treeNode.id;
    var pId = treeNode.pId;
    var name = treeNode.name;
    editName(id,pId,name);

}
function editName(id,pId,name) {
    var url = bathPath+'/updateGroup.htmlx';
    $.post(url,{id:id,pId:pId,name:name},function (data) {
        var rs = data.msg;
        if("error"==rs){
            layer.msg('操作失败!',{icon: 5,time:1000});
        }
        if("success"==rs){
            // refreshNode();
        }

    },"json")
}

function refreshNode() {
    var zTree = $.fn.zTree.getZTreeObj("tree"),
        type = "refresh",
        silent = false,
        nodes = zTree.getSelectedNodes();
    var  node1 = nodes[0];
    zTree.reAsyncChildNodes(node1, type, silent);
}
function refreshParentNode() {
    var zTree = $.fn.zTree.getZTreeObj("tree"),
        type = "refresh",
        silent = false,
        nodes = zTree.getSelectedNodes();
    /*根据 zTree 的唯一标识 tId 快速获取节点 JSON 数据对象*/
    var parentNode = zTree.getNodeByTId(nodes[0].parentTId);
    /*选中指定节点*/
    zTree.selectNode(parentNode);
    zTree.reAsyncChildNodes(parentNode, type, silent);
}